// MonitoringUnitManager.h: interface for the MonitoringUnitManager class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_MONITORINGUNITMANAGER_H__8BC9B845_D3DC_4A10_91D5_868896D6CABA__INCLUDED_)
#define AFX_MONITORINGUNITMANAGER_H__8BC9B845_D3DC_4A10_91D5_868896D6CABA__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "MonitoringUnitModality.h"
#include "MonitoringUnitState.h"

#define SORT_BY_ID    1
#define SORT_BY_NAME  2
#define SORT_BY_TYPE  3

class BaseMonitoringUnit;
struct MonUnitData;
struct MonUnitDef;

struct MonUnitDataEx;
struct MonUnitDefEx;

class MonitoringUnitManager
{
public:
	void UpdateMonitoringUnitsAllInListCtrl(CListCtrl& listctrl, BaseMonitoringUnit * pMonitoringUnit) const;
	MonitoringUnitManager();
	virtual ~MonitoringUnitManager();

	//2005-7-5
	virtual void DoCurrRecord(BaseMonitoringUnit * pMonitoringUnit, int nRecordType) const {}

	// Overrides
	virtual void DoDisplay(BaseMonitoringUnit * pMonitoringUnit) const {}
	virtual void DoRecord(BaseMonitoringUnit * pMonitoringUnit) const {}
	virtual void DoAlarm(BaseMonitoringUnit * pMonitoringUnit) const {}
	virtual void StateChanged(BaseMonitoringUnit * pMonitoringUnit, MonitoringUnitState::States OldState) const {}

	// Implementations
	const std::vector<BaseMonitoringUnit *>& GetMonitoringUnits() const {return m_MonitoringUnits;}

	int  GetMonitoringUnits_Size() const {return m_MonitoringUnits.size();}
	void GenerateRandomDefinitionFile(const CString& strFileName, int dwNumOfDef);
	void GenerateRandomData();

	void ConvertDefFromStruct(MonUnitDef * pMonUnitDefs, DWORD dwNumOfDef);
	void ConvertDefFromStruct(MonUnitDefEx * pMonUnitDefs, DWORD dwNumOfDef);

	void ConvertDefToStruct(MonUnitDef * pMonUnitDefs, DWORD dwNumOfDef);
	void ConvertDefToStruct(MonUnitDefEx * pMonUnitDefs, DWORD dwNumOfDef);

	void ConvertDataToStruct(MonUnitData * pMonUnitDatas, DWORD dwNumOfData);
	void ConvertDataToStruct(MonUnitDataEx * pMonUnitDatas, DWORD dwNumOfData);

	bool LoadStructFromFile(const CString& strFileName, MonUnitDef *& pMonUnitDefs, DWORD& dwNumOfDef);
	bool SaveStructToFile(const CString& strFileName, MonUnitDef * pMonUnitDefs, DWORD dwNumOfDef);

	bool LoadStructFromFile(const CString& strFileName, MonUnitDefEx *& pMonUnitDefs, DWORD& dwNumOfDef);
	bool SaveStructToFile(const CString& strFileName, MonUnitDefEx * pMonUnitDefs, DWORD dwNumOfDef);

	bool LoadDefinitionFromXMLFile(const CString& strFileName);
	bool SaveDefinitionToXMLFile(const CString& strFileName);
	void Serialize(CArchive& ar);

	BaseMonitoringUnit * NewMonitoringUnitByModality(MonitoringUnitModality::Modalities Modality);
	bool HasMonitoringUnitExisted(BaseMonitoringUnit * pMonitoringUnit) const;
	void AddMonitoringUnit(BaseMonitoringUnit * pMonitoringUnit);
	void RemoveMonitoringUnit(BaseMonitoringUnit * pMonitoringUnit);
	void DeleteMonitoringUnit(BaseMonitoringUnit * pMonitoringUnit);
	void RemoveAllMonitoringUnits();
	void DeleteAllMonitoringUnits();
	std::vector<BaseMonitoringUnit *>::iterator FindMonitoringUnit(BaseMonitoringUnit * pMonitoringUnit);
	BaseMonitoringUnit * ReplaceMonitoringUnit(std::vector<BaseMonitoringUnit *>::iterator iter, BaseMonitoringUnit * pMonitoringUnit);
	void SwapMonitoringUnits(std::vector<BaseMonitoringUnit *>::iterator iter1, std::vector<BaseMonitoringUnit *>::iterator iter2);
	BaseMonitoringUnit * GetMonitoringUnitById(LPCSTR lpszId);
	void ApplyNewDefinition(MonitoringUnitManager * pMgrFrom);
	int GetFirstMonUnitCanBeShownAsHistogram() const;
	BaseMonitoringUnit * GetNextMonUnitCanBeShownAsHistogram(int& nPos) const;
	void GetAllMonUnitsCanBeShownAsHistogram(std::vector<BaseMonitoringUnit *>& MonitoringUnits) const;
	int CopyMonitoringUnitsTo(MonitoringUnitManager * pMonitoringUnitMgr, int nNumOfMonUnits) const;
	BaseMonitoringUnit * GetMonitoringUnitAt(int nIndex) const {return nIndex < m_MonitoringUnits.size()?m_MonitoringUnits[nIndex]:NULL;}
	void HaltAllMonitoringUnits(const CTime& time, bool bRecord = true);
	void SetData(BaseMonitoringUnit * pMonitoringUnit, const CTime& time, MonitoringUnitState::States State, float fValue = 0, bool bRecord = true) const;
	void SortMonUnits(int nOrderFlag = SORT_BY_ID);

	// UI utilities
	int InsertMonitoringUnitsToListCtrl(CListCtrl& listctrl) const;
	void UpdateMonitoringUnitsInListCtrl(CListCtrl& listctrl, BaseMonitoringUnit * pMonitoringUnit) const;
	int InsertMonitoringUnitsToComboBox(CComboBox& combobox) const;

protected:
	std::vector<BaseMonitoringUnit *> m_MonitoringUnits;
};

struct MonitoringUnitComparison
{
	MonitoringUnitComparison(int nCompFlag) : m_nCompFlag(nCompFlag) {}

	bool operator()(const BaseMonitoringUnit * pMonitoringUnit1, const BaseMonitoringUnit * pMonitoringUnit2);

	int m_nCompFlag;
};

#endif // !defined(AFX_MONITORINGUNITMANAGER_H__8BC9B845_D3DC_4A10_91D5_868896D6CABA__INCLUDED_)
